package com.hscm.boss.mvc.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hscm.boss.common.consants.SysConstants;
import com.hscm.boss.common.enums.ResponseCode;
import com.hscm.boss.convert.UserConvert;
import com.hscm.boss.dao.entity.UserInfo;
import com.hscm.boss.mvc.model.AjaxResult;
import com.hscm.boss.mvc.model.from.user.UserEditFrom;
import com.hscm.boss.mvc.model.pages.PageInfo;
import com.hscm.boss.mvc.model.vo.user.UserInfoVo;
import com.hscm.boss.mvc.service.UserService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;


@RestController
public class UserController extends BaseController {


    @Resource
    private UserService userService;

    @Resource
    private UserConvert userConvert;

    //分页查询商户
    @GetMapping("/user/pageList")
    public AjaxResult pageList(Integer pageNo) {
        pageNo = defPageNo(pageNo);
        IPage<UserInfo> pageResult = userService.pageList(pageNo, SysConstants.PAGE_SIZE);

        //这是获取总页数
        long totalPage = pageResult.getPages();

        //本次分页查询出来的记录
        List<UserInfo> userInfolist = pageResult.getRecords();

        //组装page 和 list
        //创建分页对象
        PageInfo pageInfo = new PageInfo(pageResult.getCurrent(), totalPage);

       // 演示前端的遮罩
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        //包装数据
        HashMap<String, Object> data = new HashMap<>();
        data.put("page", pageInfo);
        data.put("list", userConvert.toListUserInfoVo(userInfolist));
        return AjaxResult.ok(data);

    }


    /*编辑商户 */
    @PostMapping("/user/edit")
    public AjaxResult editUser(@RequestBody @Valid UserEditFrom userEditFrom){
           userService.editUser(userEditFrom);
           return AjaxResult.ok();
    }

    /*按主键查询*/
    @GetMapping("/user/id")
    public AjaxResult queryById(@RequestParam String userId){
        if (StrUtil.isNotBlank(userId)){
            UserInfo userInfo = userService.queryById(userId);
            UserInfoVo userInfoVo = userConvert.mapUserInfoVo(userInfo);
            return AjaxResult.ok(userInfoVo);
        }
        return AjaxResult.fail(ResponseCode.REQUEST_PARMA_ERR);
    }

    /*修改状态*/
    @PostMapping("/user/status")
    private AjaxResult editStatus(@RequestParam String userNo){
        if(StrUtil.isNotBlank(userNo)){
             userService.editStatus(userNo);
            return AjaxResult.ok();
        }
        return AjaxResult.fail(ResponseCode.REQUEST_PARMA_ERR);
    }



}
